package com.trs.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import com.hierynomus.msdtyp.AccessMask;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.mssmb2.SMB2CreateDisposition;
import com.hierynomus.mssmb2.SMB2CreateOptions;
import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;

public class SmbUtil {

	public static void main(String[] args) {

		SMBClient client = null;
		Session session = null;
		DiskShare diskShare = null;
		Connection connection = null;
		AuthenticationContext authenticationContext = null;

		try {
			client = new SMBClient();
			connection = client.connect("172.16.102.238");
			// 创建连接会话.
			authenticationContext = new AuthenticationContext("sunzhihuan", "qwer1234!".toCharArray(), null);
			session = connection.authenticate(authenticationContext);
			// 操作共享文件.
			diskShare = (DiskShare) session.connectShare("share");
			List<String> list = new ArrayList<>();
			ReadNasFepkRecurisive(diskShare.getSmbPath().toString(), "", list, diskShare);
			System.err.println(list);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (null != diskShare) {
					diskShare.close();
				}
				if (null != session) {
					session.close();
				}
				if (null != client) {
					client.close();
				}
			} catch (Exception ex) {
			}
		}

	}

	// 递归调用
	public static void ReadNasFepkRecurisive(String fatherPath, String sharePath, List<String> filelist, DiskShare share) {
		System.err.println(sharePath);
		List<FileIdBothDirectoryInformation> list = share.list(sharePath);
		for (FileIdBothDirectoryInformation file : list) {
			String filename = file.getFileName();
			if (".".equals(filename) || "..".equals(filename)) {
				continue;
			}
			if (EnumWithValue.EnumUtils.isSet(file.getFileAttributes(), FileAttributes.FILE_ATTRIBUTE_DIRECTORY)) {
				// 说明是文件夹
				String sonPath = fatherPath + "/" + filename;
				// 下一级路径
				sharePath = sharePath == "" ? filename : sharePath + "/" + filename;
				// 递归读取
				ReadNasFepkRecurisive(sonPath, sharePath, filelist, share);
				// 读取完后恢复上一级路径
				if (sharePath.lastIndexOf("/") != -1) {
					sharePath = sharePath.substring(0, sharePath.lastIndexOf("/"));
				} else {
					sharePath = "";
				}
			} else {
				// 说明是文件
				filelist.add(fatherPath + "/" + filename);
				String sharepath = (fatherPath + "/" + filename).replace(share.getSmbPath().toString() + "/", "");
				com.hierynomus.smbj.share.File jsonfile = share.openFile(sharepath, EnumSet.of(AccessMask.GENERIC_READ), EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL), SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN_IF, EnumSet.noneOf(SMB2CreateOptions.class));
				System.err.println("filename : " + sharepath);
				System.err.println(readJson(jsonfile.getInputStream()));
			}
		}
	}

	public static String readJson(InputStream inputStream) {
		StringBuffer strBuffer = new StringBuffer();
		String encoding = "UTF-8"; // 文本编码
		InputStreamReader read;
		try {
			read = new InputStreamReader(inputStream, encoding);
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				strBuffer.append(lineTxt);
			}
			read.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strBuffer.toString();
	}
}
